package com.hsmw.api.config.advice;

import com.hsmw.api.exception.ApiException;
import com.htn.common.core.base.DataResponse;
import com.htn.common.core.constant.CommonConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author WD
 * @version 1.0
 * @description: TODO
 * @date 2021/1/26 14:39
 */
@RestControllerAdvice
@Slf4j
public class ExceptionControllerAdvice {


	@ExceptionHandler(MethodArgumentNotValidException.class)
	public DataResponse MethodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
		// 从异常对象中拿到ObjectError对象
		ObjectError objectError = e.getBindingResult().getAllErrors().get(0);
		// 然后提取错误提示信息进行返回
		return new DataResponse(CommonConstants.ResponseStatus.VALIDATE_FAILED, objectError.getDefaultMessage());
	}

	@ExceptionHandler(ApiException.class)
	public DataResponse ApiExceptionHandler(ApiException e) {
		e.getStackTrace();
		log.error("错误的信息： {}", e.getMessage());
		return new DataResponse(e, e.getResponseStatus());
	}

//
//	@ExceptionHandler(Exception.class)
//	public DataResponse ExceptionHandler(Exception exception) {
//		log.error("错误日志的信息是：{}", exception.getMessage());
//		System.out.println(exception.getStackTrace());
//		System.out.println(exception.getCause());
//		System.out.println(exception.getLocalizedMessage());
//		System.out.println(exception.getSuppressed());
//		log.error("错误日志打印结束	END");
//		return DataResponse.failure(CommonConstants.ResponseStatus.FAIL);
//	}

}
